
#import	<stdlib.h>
#import	<math.h>
#import	<appkit/Application.h>
#import	<appkit/Slider.h>
#import	<appkit/Button.h>
#import	<appkit/NXImage.h>
#import <appkit/color.h>
#import	<dpsclient/wraps.h>
#import	"QixColorView.h"

/**********************************************************************/

#define	LEFT		( 100 )
#define	RIGHT		( 101 )
#define	UP			( 102 )
#define	DOWN		( 103 )

#define	INITLEN		( 55 )				//	Initial qix tail length.

#define	A_BASE_INC	( 5 )				//	Default distance to move the
										//	"A" point of a qix structure.
#define	B_BASE_INC	( 8 )				//	Default distance to move the
										//	"B" point of a qix structure.

/**********************************************************************/


@implementation QixColorView

/**********************************************************************/

- newWindow
{
	[ self resetQix : &head : NO ];
	[ self resetQix : &tail : YES ];
	
	return self;
}
 
/**********************************************************************/

- (const char *) windowTitle
{
	return ( const char * ) "Qix Lines";
}

/**********************************************************************/

- initFrame : ( const NXRect * ) frameRect
{
	[ super initFrame : frameRect ];
	
	[ self setOpaque : YES ];
	[ self setClipping : NO ];
	
	[ self resetQix : &head : NO ];
	[ self resetQix : &tail : YES ];
	
	// added for color by Don Yacktman 11/7/91
	hue = 0; // starting hue
	
	return self;
}

/**********************************************************************/

- sizeTo : ( NXCoord ) width : ( NXCoord ) height
{
	[ super sizeTo : width : height ];
	
	[ self resetQix : &head : NO ];
	[ self resetQix : &tail : YES ];
	
	return self;
}

/**********************************************************************/

- resetQix : ( QIX * ) qix : ( BOOL ) resetControls
{
	if( resetControls == YES )
		tailLen = INITLEN;
	
	qix->pointA.x = bounds.size.width / 3.0;
	qix->pointA.y = bounds.size.height / 3.0;
	qix->pointA.x_dir = RIGHT;
	qix->pointA.y_dir = DOWN;
	qix->pointA.x_inc = A_BASE_INC;
	qix->pointA.y_inc = A_BASE_INC;
	qix->pointA.orig_inc = A_BASE_INC;
	
	qix->pointB.x = bounds.size.width / 2.0;
	qix->pointB.y = bounds.size.height / 2.0;
	qix->pointB.x_dir = LEFT;
	qix->pointB.y_dir = UP;
	qix->pointB.x_inc = B_BASE_INC;
	qix->pointB.y_inc = B_BASE_INC;
	qix->pointB.orig_inc = B_BASE_INC;
	
	return self;
}

/**********************************************************************/
	
- setQixPoint : ( MVPOINT * ) point
{
	if( point->x >= bounds.size.width )
	{
		point->x_dir = LEFT;
		point->x_inc = point->orig_inc;
	}
	else if( point->x <= 0 )
		point->x_dir = RIGHT;
	
	if( point->x_dir == RIGHT )
	{
		point->x += point->x_inc;
		point->x_inc -= .009;
	}
	else
	{
		point->x -= point->x_inc;
		point->x_inc += .03;
	}

	if( point->y >= bounds.size.height )
	{
		point->y_dir = DOWN;
		point->y_inc = point->orig_inc;
	}
	else if( point->y <= 0 )
		point->y_dir = UP;
		
	if( point->y_dir == UP )
	{
		point->y += point->y_inc;
		point->y_inc -= .009;
	}
	else
	{
		point->y -= point->y_inc;
		point->y_inc += .06;
	}
		
	return self;
}

/**********************************************************************/

- drawQix : ( QIX ) qix
{
	PSsetlinewidth( 0.5 );

	PSmoveto( qix.pointA.x, qix.pointA.y );
	PSlineto( qix.pointB.x, qix.pointB.y );
	PSstroke( );
	
	return self;
}

/**********************************************************************/

- oneStep
{
	NXColor drawColor; // added 11/7/91 by Don Yacktman for COLOR
	
	if( tailLen )
		--tailLen;
	else
	{
		PSsetgray( NX_BLACK );
		[ self drawQix : tail ];
		[ self setQixPoint :  &tail.pointA ];
		[ self setQixPoint :  &tail.pointB ];
	}
	
	// PSsetgray( NX_WHITE );
	// Next three lines for COLOR added 11/7/91 by Don Yacktman
	drawColor = NXConvertHSBToColor(hue/256.0, 1.0, 1.0);
	hue = (hue + 1) % 256;
	NXSetColor(drawColor);
	
	[ self drawQix : head ];
	[ self setQixPoint : &head.pointA ];
	[ self setQixPoint : &head.pointB ];
	
	return self;
}

/**********************************************************************/

- drawSelf : ( NXRect * ) r : ( int ) count
{	 
	if (!r || !count)
		return self;
		
	PSsetgray( NX_BLACK );
	
	NXRectFill( r );
	
	return self;
	
}

/**********************************************************************/

@end
